package cn.heygod.graywhale.modules.ipaas.controller;

import com.github.pagehelper.PageInfo;
import cn.heygod.graywhale.common.context.UserContext;
import cn.heygod.graywhale.modules.ipaas.domain.GwCustomProcessor;
import cn.heygod.graywhale.common.tools.string.Convert;
import cn.heygod.graywhale.common.web.base.BaseController;
import cn.heygod.graywhale.common.web.domain.request.PageDomain;
import cn.heygod.graywhale.common.web.domain.response.Result;
import cn.heygod.graywhale.common.web.domain.response.module.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import cn.heygod.graywhale.modules.ipaas.service.IGwCustomProcessorService;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 自定义处理器Controller
 *
 * @author simon
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/ipaas/customProcessor")
public class GwCustomProcessorController extends BaseController {

    private String prefix = "ipaas/customProcessor";

    @Autowired
    private IGwCustomProcessorService gwCustomProcessorService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/main','ipaas:customProcessor:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询自定义处理器列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/data','ipaas:customProcessor:data')")
    public ResultTable list(@ModelAttribute GwCustomProcessor gwCustomProcessor, PageDomain pageDomain) {
        PageInfo<GwCustomProcessor> pageInfo = gwCustomProcessorService.selectGwCustomProcessorPage(gwCustomProcessor, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增自定义处理器
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/add','ipaas:customProcessor:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增自定义处理器
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/add','ipaas:customProcessor:add')")
    public Result save(@RequestBody GwCustomProcessor gwCustomProcessor) {
        return decide(gwCustomProcessorService.save(gwCustomProcessor));
    }

    /**
     * 修改自定义处理器
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/edit','ipaas:customProcessor:edit')")
    public ModelAndView edit(String id, ModelMap map) {
        GwCustomProcessor gwCustomProcessor =gwCustomProcessorService.getById(id);
        map.put("gwCustomProcessor", gwCustomProcessor);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改自定义处理器
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/edit','ipaas:customProcessor:edit')")
    public Result update(@RequestBody GwCustomProcessor gwCustomProcessor) {
        return decide(gwCustomProcessorService.updateById(gwCustomProcessor));
    }

    /**
     * 删除自定义处理器
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/remove','ipaas:customProcessor:remove')")
    public Result batchRemove(String ids) {
        return decide(gwCustomProcessorService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除自定义处理器
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/remove','ipaas:customProcessor:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(gwCustomProcessorService.removeById(id));
    }

    /**
     * 获取自定义处理器
     */
    @ResponseBody
    @DeleteMapping("/get/{id}")
    @PreAuthorize("hasPermission('/ipaas/customProcessor/get','ipaas:customProcessor:get')")
    public Result get(@PathVariable("id") String id) {
        GwCustomProcessor gwCustomProcessor =gwCustomProcessorService.getById(id);
        return Result.success(gwCustomProcessor);
    }
}
